﻿using AntdUI;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Super.Admin.Enums;
using Super.Admin.Global;
using Super.Admin.IServices;
using System.Drawing.Text;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Super.Admin.Utils.ThemeManager
{
    /// <summary>
    /// 主题设置
    /// </summary>
    public sealed class ThemeSettingHelper
    {
        /// <summary>
        /// 主题配置
        /// </summary>
        public ThemeConfig ThemeConfig { get; set; } = new ThemeConfig();
        public static readonly Lazy<ThemeSettingHelper> _instance = new Lazy<ThemeSettingHelper>(() => new ThemeSettingHelper());
        public static ThemeSettingHelper Instance => _instance.Value;
        private ThemeSettingHelper() { }
        /// <summary>
        /// 字体集合
        /// </summary>
        public PrivateFontCollection FontList = new PrivateFontCollection();
        /// <summary>
        /// 是否开始应用主题
        /// </summary>
        public bool IsApplyTheme { get; set; } = false;
        /// <summary>
        /// 切换白天和黑夜主题
        /// </summary>
        /// <param name="themetype">类型</param>
        public void SwitchThemeDrakOrLight(TAMode themetype)
        {
            ThemeConfig.AppearanceSettings.Theme.ThemeType = themetype;
            foreach (Form form in Application.OpenForms)
            {
                if (form is Views.Base.BaseForm baseForm)
                {
                    baseForm.RefreshTheme(form,themetype);
                }
            }
        }
        /// <summary>
        /// 切换主题颜色
        /// </summary>
        /// <param name="color"></param>
        public void SwitchThemeColor(ThemeColorType colorType, string color)
        {
            if (string.IsNullOrEmpty(color)) return;
            if (!color.StartsWith('#')) color = "#" + color;
            switch (colorType)
            {
                case ThemeColorType.Success:
                    ThemeConfig.AppearanceSettings.BuiltInThemes.SuccessColor = color;
                    break;
                case ThemeColorType.Primary:
                    ThemeConfig.AppearanceSettings.BuiltInThemes.PrimaryColor = color;
                    break;
                case ThemeColorType.Warning:
                    ThemeConfig.AppearanceSettings.BuiltInThemes.WarningColor = color;
                    break;
                case ThemeColorType.Info:
                    ThemeConfig.AppearanceSettings.BuiltInThemes.InfoColor = color;
                    break;
                case ThemeColorType.Error:
                    ThemeConfig.AppearanceSettings.BuiltInThemes.ErrorColor = color;
                    break;
            }
            foreach (Form form in Application.OpenForms)
            {
                if (form is Views.Base.BaseForm baseForm)
                {
                    baseForm.Refresh();
                }
            }
        }
        /// <summary>
        /// 切换主题圆角
        /// </summary>
        /// <param name="cornerRadius"></param>
        public void SwitchThemeCornerRadius(int cornerRadius)
        {
            ThemeConfig.AppearanceSettings.CornerRadiusType.CornerRadius = cornerRadius;
        }
        /// <summary>
        /// 切换登录布局
        /// </summary>
        /// <param name="layout"></param>
        public void SwitchLoginLayout(string layout)
        {
            if (string.IsNullOrEmpty(layout)) return;
            LoginLayoutType loginType = LoginLayoutType.Left;
            bool res = Enum.TryParse<LoginLayoutType>(layout, out loginType);
            if (res)
            {
                ThemeConfig.LayoutSettings.LoginLayout.LoginLayoutType = loginType;
            }
        }
        /// <summary>
        /// 保存设置
        /// </summary>
        public void Save()
        {
            var json = JsonConvert.SerializeObject(ThemeConfig, Formatting.Indented);
            File.WriteAllText(FilePath.ThemeSettingFilePath, json);
        }
        /// <summary>
        /// 应用主题配置
        /// </summary>
        public void ApplyTheme()
        {
            IsApplyTheme = false;
            if (!File.Exists(FilePath.ThemeSettingFilePath))
            {
                JsonHelper.SaveObjectToJsonFile(new ThemeConfig(), FilePath.ThemeSettingFilePath);
            }
            var json = File.ReadAllText(FilePath.ThemeSettingFilePath);
            ThemeConfig = JsonHelper.GetObjectFromJsonString<ThemeConfig>(json);
            IsApplyTheme = true;
            // 加载字体图标
            var font = ResourceHelper.Instance.LoadFont(Global.AppContext.Instance.IconFont);
            FontList.AddMemoryFont(font.fontPtr, font.fontLength);
        }
        /// <summary>
        /// 切换语言 中英切换
        /// </summary>
        /// <param name="language"></param>
        public void SwitchLanguage(string language)
        {
            if (string.IsNullOrEmpty(language)) return;
            Enum.TryParse<LanguageType>(language, out LanguageType languageType);
            ThemeConfig.GeneralSettings.General.Language = languageType;
        }
        /// <summary>
        /// 切换主题布局
        /// </summary>
        /// <param name="mainLayout"></param>
        public void SwitchThemeMainLayout(string mainLayout)
        {
            if (string.IsNullOrEmpty(mainLayout)) return;
            NavigationPosition navigationPosition = NavigationPosition.HorizontalMenu;
            bool res = Enum.TryParse<NavigationPosition>(mainLayout, out navigationPosition);
            if (res)
            {
                ThemeConfig.LayoutSettings.Layout.NavigationPosition = navigationPosition;
            }
        }

        // 统一设置主题 包括子控件
        public void SetControlsTheme(Control control, TAMode themeMode)
        {
            if (control is AntdUI.IControl antdUi)
            {
                antdUi.ColorScheme = themeMode;
            }
            foreach (Control child in control.Controls)
            {
                SetControlsTheme(child, themeMode);
            }
        }
    }
}
